package com.youjiang.meeting.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youjiang.meeting.mapper.ProgramMapper;
import com.youjiang.meeting.mapper.RedpackMapper;
import com.youjiang.meeting.mapper.ThegameMapper;
import com.youjiang.meeting.mapper.VoitMapper;
import com.youjiang.meeting.po.Program;
import com.youjiang.meeting.po.ProgramExample;
import com.youjiang.meeting.po.Redpack;
import com.youjiang.meeting.po.RedpackExample;
import com.youjiang.meeting.po.Thegame;
import com.youjiang.meeting.po.ThegameExample;
import com.youjiang.meeting.po.UserInfo;
import com.youjiang.meeting.po.Voit;
import com.youjiang.meeting.po.VoitExample;
import com.youjiang.meeting.service.ControlService;
import com.youjiang.meeting.service.ManagermentService;
import com.youjiang.meeting.service.WeiXinMassegeService;
import com.youjiang.meeting.utils.ToolUtils;
import com.youjiang.meeting.utils.weixin.WeixinSignUtil;

import net.sf.json.JSONObject;

/**
 * <p>
 * Title: ManagermentServiceImpl
 * </p> 
 * <p>
 * Description:TODO
 * </p> 
 */
@Service
public class ManagermentServiceImpl implements ManagermentService{
	@Resource
	private ProgramMapper programMapper;
	@Resource
	private ThegameMapper thegameMapper;
	@Resource
	private RedpackMapper redpackMapper;
	@Resource
	private ControlService controlService;
	@Autowired
	private VoitMapper voitMapper;
	@Resource
	private WeiXinMassegeService weixinService;
	@Override
	public String addProgram(int userid,String name, String principal) {
		JSONObject json=new JSONObject();
		try{
			if(StringUtils.isEmpty(name)||StringUtils.isEmpty(principal)){
				json.put("code", "0");
				json.put("des", "节目名和负责人不能为空");
				return json.toString();

			}
			Program program=new Program();
			program.setName(name);
			program.setPrincipal(principal);
			program.setUserid(userid);
			programMapper.insert(program);
			Voit record = new Voit();
			record.setLevel(0);
			record.setProgramid(program.getId());
			record.setKeyword("投票");
			System.out.println("record-------"+record.getProgramid());
			voitMapper.insert(record);
			
			return ToolUtils.json(json, "1", "添加成功");
		}catch (Exception e){
			e.printStackTrace();
			json.put("code", "2");
			json.put("des", "数据出错");
			return json.toString();
		}
	}

	@Override
	public String delProgram(HttpSession session,Integer id) {
		JSONObject json=new JSONObject();
		UserInfo info = (UserInfo) session.getAttribute("userInfo");
		if(info==null){
			return ToolUtils.json(json, "00", "登录超时");
		}
		try{
			ProgramExample example = new ProgramExample();
			ProgramExample.Criteria criteria = example.createCriteria();
			criteria.andUseridEqualTo(info.getId());
			criteria.andIdEqualTo(id);
			List<Program> pList = programMapper.selectByExample(example);
			if(pList!=null && pList.size()>0){
				VoitExample voitExample = new VoitExample();
				VoitExample.Criteria vcriteria = voitExample.createCriteria();
				vcriteria.andProgramidEqualTo(id);
				List<Voit> voitList = voitMapper.selectByExample(voitExample);
			    if(voitList!=null && voitList.size()>0){
			    	Voit voit = voitList.get(0);
			    	if(voit!=null){
			    		if(voit.getLevel()==1){
			    			return ToolUtils.json(json, "10", "开始投票进行中,不能删除,请先结束投票");
			    		}
			    	}
			    }
			}

		    programMapper.deleteByPrimaryKey(id);
			return ToolUtils.json(json, "11", "删除成功");
		}catch(Exception e){
			e.printStackTrace();
			json.put("code", 01);
			json.put("des", "删除失败");
			return json.toString();
		}
		
	}

	@Override
	public String addGame(String name, String answer) {
		JSONObject json=new JSONObject();
		try{
			if(StringUtils.isEmpty(name)||StringUtils.isEmpty(answer)){
				json.put("code", 0);
				json.put("msg", "题目和答案不能为空");
				return json.toString();
			}
			Thegame game=new Thegame();
			game.setAnswer(answer);
			game.setName(name);
			thegameMapper.insert(game);
			json.put("code", 1);
			json.put("des", "添加成功");
			return json.toString();
		}catch(Exception e){
			e.printStackTrace();
		}
		json.put("code", 0);
		json.put("des", "数据出错");
		return json.toString();
	}

	@Override
	public String delGame(Integer id) {
		JSONObject json=new JSONObject();
		try{
			if(id==controlService.getGame()){
				return ToolUtils.json(json, "0", "竞猜游戏进行中不可删除");
			}
			thegameMapper.deleteByPrimaryKey(id);
			return ToolUtils.json(json, "1", "删除成功");
		}catch(Exception e){
			e.printStackTrace();
			return ToolUtils.json(json, "0", "数据出错");
		}
	}

	@Override
	public String addRedPack(int userid,Redpack redpack) {
		JSONObject json=new JSONObject();
		try{
			redpack.setUserid(userid);
			redpack.setLevel(0);
			redpackMapper.insert(redpack);
			return ToolUtils.json(json, "01", "添加成功");
		}catch(Exception e){
			e.printStackTrace();
			json.put("code", 0);
			json.put("des", "数据出错");
			
			return json.toString();
		}
	}

	@Override
	public String delRedPack(HttpSession session,Integer id) {
		JSONObject json=new JSONObject();
		UserInfo info = (UserInfo) session.getAttribute("userInfo");
		if(info==null){
			return ToolUtils.json(json, "00", "登录超时");
		}
		try{			
			Redpack redpack = redpackMapper.selectByPrimaryKey(id);
			if(redpack.getLevel()==1){
				json.put("code", 01);
				json.put("msg", "红包进入中，无法删除,请先结束红包");
				return json.toString();
			}			
			if(redpack.getLevel()==-1 || redpack.getLevel()==0){
				redpack.setLevel(2);
				redpackMapper.updateByPrimaryKey(redpack);
				return ToolUtils.json(json, "02", "删除成功");
			}			
		}catch(Exception e){
			e.printStackTrace();
			json.put("code", 10);
			json.put("msg", "数据出错");
			return json.toString();
		}
		json.put("code", 01);
		json.put("des", "红包删除失败");
		return json.toString();

	}

	@Override
	public String login(String username,HttpServletRequest request) {
		try{
			InputStream inputStream = ManagermentServiceImpl.class.getResourceAsStream("/weixin.properties");
			Properties pr=new Properties();
			pr.load(inputStream);
			String password = pr.getProperty("passwrod");
			 String md5 = WeixinSignUtil.resultMD5(password, false);
			 if(md5.equals(username)){
				 return "admin";
			 }else{
				 request.setAttribute("des", "密钥出错");
				 return "login";
			 }
		}catch(Exception e){
			e.printStackTrace();
		}
		 request.setAttribute("des", "未知错误");
		 return "login";
	}
	@Override
	public String selectProgram(HttpSession session) {
		JSONObject json=new JSONObject();
		UserInfo info = (UserInfo) session.getAttribute("userInfo");
		if(info==null){
			return ToolUtils.json(json, "00", "登录超时");
		}
		/**
		 * 当点击界面按钮时，会先查看当前数据库中是否还有为完成的红包，如果有则弹出一个提示界面让用户去结束红包，然后
		 * 用户可以点击其它节目按钮
		 */
		RedpackExample redexample = new RedpackExample();
		RedpackExample.Criteria criteria = redexample.createCriteria();
		criteria.andUseridEqualTo(info.getId());
		List<Redpack> redList = redpackMapper.selectByExample(redexample);
		boolean flag = false;
		if(redList!=null && redList.size()>0){
			for(Redpack pack:redList){
				if(pack.getLevel()==1){
					flag = true;
					break;
				}
			}
			if(flag){
				return ToolUtils.json(json, "02", "当前还有未结束的红包，请先结束上一轮红包");
			}
		}
		try{
			int userid = info.getId();
			ProgramExample example = new ProgramExample();
			ProgramExample.Criteria pcriteria = example.createCriteria();
			pcriteria.andUseridEqualTo(userid);
			
			List<Program> selectByExample = programMapper.selectByExample(example);
			List<Program> pList = new ArrayList<>();
			for(Program p:selectByExample){
				if(p!=null){
					pList.add(p);
				    	
				}
			}
			
			json.put("code", 1);
			json.put("data", pList);
			return json.toString();
		}catch(Exception e){
			e.printStackTrace();
			return ToolUtils.json(json, "01", "未知错误");
		}
	}

	@Override
	public String selectGame() {
		JSONObject json=new JSONObject();
		try{
			List<Thegame> selectByExample = thegameMapper.selectByExample(new ThegameExample());
			json.put("code", 1);
			json.put("data", selectByExample);
			return json.toString();
		}catch(Exception e){
			e.printStackTrace();
			return ToolUtils.json(json, "0", "未知错误");
		}

	}

	public String selectRedPack(HttpSession session) {
		JSONObject json=new JSONObject();
		try{
			
			UserInfo info = (UserInfo) session.getAttribute("userInfo");
			json.put("usfe", "sa"+info);
			if(info==null){
				return ToolUtils.json(json, "00", "用户登录超时");

			}
			RedpackExample exanple = new RedpackExample();
			RedpackExample.Criteria criteria = exanple.createCriteria();
			criteria.andUseridEqualTo(info.getId());
			criteria.andLevelNotEqualTo(2);
			List<Redpack> selectByExample = redpackMapper.selectByExample(exanple);
			json.put("code", 1);
			json.put("data", selectByExample);
			return json.toString();
		}catch(Exception e){
			e.printStackTrace();
			return ToolUtils.json(json, "01", "未知错误");
		}
	}
	@Override
	public void exportInfo(HttpServletResponse res) {
		
		
	}

}
